﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace L.M.System.Application.Services
{
    /// <summary>
    /// 单位
    /// </summary>
    public class Table_of_unitsServices : BaseServices<table_of_units, string>, ITable_of_unitsServices
    {
        private readonly ITable_of_unitsRepository table_Of_UnitsRepository;

        public Table_of_unitsServices(ITable_of_unitsRepository table_Of_UnitsRepository) : base(table_Of_UnitsRepository)
        {
            this.table_Of_UnitsRepository = table_Of_UnitsRepository;
        }

        /// <summary>
        /// 异步分页查询单位表
        /// </summary>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前也索引</param>
        /// <param name="tName">模糊查询</param>
        /// <returns>异步分页查询单位表</returns>
        public async Task<ApiReturn<PageETO<table_of_units>>> GetTable_of_unitsAsync(int pageSize, int pageIndex, string? tName)
        {
            ApiReturn<PageETO<table_of_units>> apiReturn = new ApiReturn<PageETO<table_of_units>>();
            Expression<Func<table_of_units, bool>> i = c => true;
            if (!string.IsNullOrEmpty(tName))
            {
                i = c => c.unit_name.Contains(tName);
            }
            var list = await table_Of_UnitsRepository.GetShowQueryableAsync(pageSize, pageIndex, i);
            apiReturn.Code = 200;
            apiReturn.Message = "成功";
            apiReturn.ObjInfor = list;
            return apiReturn;
        }

        /// <summary>
        /// 单位添加
        /// </summary>
        /// <param name="unit_AddDTO">单位DTO</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> Addtable_Of_Units(Unit_AddDTO unit_AddDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            if (unit_AddDTO.unit_name =="")
            {
                apiReturn.Code = 400;
                apiReturn.Message = "请输入单位名称";
                return apiReturn;
            }
            var res = await table_Of_UnitsRepository.GetShowObjAsync(c => c.unit_name == unit_AddDTO.unit_name);
            if(res == null)
            {
                table_of_units table_Of_Units = new table_of_units();
                table_Of_Units.unit_number = YitIdHelper.NextId().ToString();
                table_Of_Units.unit_name = unit_AddDTO.unit_name;
                table_Of_Units.unit_operator = unit_AddDTO.unit_operator;
                table_Of_Units.unit_status = unit_AddDTO.unit_status;
                await table_Of_UnitsRepository.AddInfor_Async(table_Of_Units);
            }
            else
            {
                apiReturn.Code = 500;
                apiReturn.Message = "已有该单位";
                return apiReturn;
            }
            apiReturn.Code = 200;
            apiReturn.Message = "添加成功";
            return apiReturn;
        }

        /// <summary>
        /// 修改单位名称
        /// </summary>
        /// <param name="unInforUpdDTO">修改的DTO</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> UpdInforUnInfor(UnInforUpdDTO unInforUpdDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            if (unInforUpdDTO.unName == "" || unInforUpdDTO.unName == null)
            {
                apiReturn.Code = 400;
                apiReturn.Message = "请输入修改后的名称";
                return apiReturn;
            }
            var res = await table_Of_UnitsRepository.GetShowObjAsync(c => c.unit_number == unInforUpdDTO.unId);
            if(res == null)
            {
                apiReturn.Code = 400;
                apiReturn.Message = "请选择一项单位";
                return apiReturn;
            }
            res.unit_name = unInforUpdDTO.unName;
            res.unit_status = unInforUpdDTO.unStaty;
            await table_Of_UnitsRepository.UpdInfor_Async(res);
            apiReturn.Code = 200;
            apiReturn.Message = "修改成功";
            return apiReturn;

        }
    }
}
